classdef Packet < handle & matlab.mixin.Heterogeneous
    %PACKET Summary of this class goes here
    %   Detailed explanation goes here
    
    properties (SetAccess = 'public', GetAccess = 'public')
        id = 0;
        size_in_bytes = 0;
        
        src_node = 0;
        dest_node = 0;
        
        current_node = 0;          %used for routing
        next_node = 0;                  %used for routing
        
        route = [];
        
        %times 
        creation_time_in_msec = 0;
        delivery_time_in_msec = 0;
        
        should_be_discarded = false;
        is_packet_transmission_complete = false;
    end
    
    methods
        function obj = Packet(id, size_in_bytes, src_node, dest_node, route)
            if nargin > 0
                obj.id = id;
                obj.size_in_bytes = size_in_bytes;
                obj.src_node = src_node;
                obj.dest_node  = dest_node;
                obj.route = route;
                ck = ClockSingleton.instance();
                obj.creation_time_in_msec = ck.current_time_in_msec;
            end
        end
        
        function discard_packet(obj)
            obj.should_be_discarded = true;
        end
        
        %CR - code a normat cctor
        function new_packet = duplicate_packet(obj)
            new_packet = Packet();
            new_packet.id = obj.id;
            new_packet.size_in_bytes = obj.size_in_bytes;
            new_packet.src_node = obj.src_node;
            new_packet.dest_node = obj.dest_node;
            new_packet.current_node = obj.current_node;
            new_packet.next_node = obj.next_node;
            new_packet.route = obj.route;
            new_packet.creation_time_in_msec = obj.creation_time_in_msec;
            new_packet.delivery_time_in_msec = obj.delivery_time_in_msec;
        end
        
        function add_node_to_packet_route(obj, node_id)
            for i=1:length(obj.route)
                if obj.route(i) == node_id
                    warning('detected circle in packet route');
                end
            end
            obj.route = [obj.route node_id];
        end
       end
end

